//
//  SearchShare.m
//  danpin
//
//  Created by chuangjia on 16/9/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "SearchShare.h"
#import "viewconfig.h"

@implementation SearchShare
+ (instancetype)sharedInstance
{
    static SearchShare *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SearchShare alloc] init];
    });
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}
-(void)dealloc
{
    DLog(@"%s", __PRETTY_FUNCTION__);
}
//
//
-(void)clearHistoryForSearch
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:nil forKey:@"searchHistory"];
    [userDefaults synchronize];
}
-(NSMutableArray*)getHistoryForSearch
{
    id userdefaults = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchHistory"];
    if (userdefaults && [userdefaults isKindOfClass:[NSMutableArray class]]) {
        NSMutableArray *arr =[NSMutableArray arrayWithArray:userdefaults];
        NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:15];
        [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if (idx<15) {
                [tempArray addObject:obj];
            }
        }];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:tempArray forKey:@"searchHistory"];
        [userDefaults synchronize];
        
        [tempArray addObject:@"清除搜索历史"];
        return tempArray;
    }
    return nil;
}

-(void)saveHistoryForSearch:(NSString *)searchkey
{
    if (searchkey==nil || searchkey.length==0) {
        return;
    }
    id userdefaults = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchHistory"];
    if (userdefaults && [userdefaults isKindOfClass:[NSMutableArray class]]) {
        NSMutableArray *temarray =[NSMutableArray arrayWithArray:userdefaults];
        for (id tempDict in temarray) {
            if (tempDict && [tempDict isKindOfClass:[NSDictionary class]]) {
                NSString *kv =[NSString stringWithFormat:@"%@",[tempDict objectForKey:@"name"]];
                if ([kv isEqualToString:searchkey]) {
                    return;
                }
            }
        }
        NSMutableDictionary *dict =[NSMutableDictionary dictionary];
        [dict setValue:searchkey forKey:@"name"];
        [dict setValue:@"0" forKey:@"selected"];
        
        [temarray insertObject:dict atIndex:0];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:temarray forKey:@"searchHistory"];
        [userDefaults synchronize];
    }else
    {
        NSMutableArray *keyDict =[NSMutableArray array];
        NSMutableDictionary *dict =[NSMutableDictionary dictionary];
        [dict setValue:searchkey forKey:@"name"];
        [dict setValue:@"0" forKey:@"selected"];
        
        [keyDict insertObject:dict atIndex:0];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:keyDict forKey:@"searchHistory"];
        [userDefaults synchronize];
    }
}
@end
